var express = require('express')
var fs = require('fs')
var path = require('path')
var jwt = require('jsonwebtoken')
var userRouter = express.Router()
var userRouter = express.Router()


//登录
userRouter.post('/login', (req, res) => {
    var { phone, pass } = req.body
    var filepath = path.join(__dirname, '../json/users.json')
    var arr = fs.readFileSync(filepath)
    arr = arr.toString() ? JSON.parse(arr) : []

    var i = arr.findIndex((item, index) => {
        return item.phone == phone && item.pass == pass
    })
    if (i != -1) {
        var token = jwt.sign({ phone }, "1234", { algorithm: 'HS256', expiresIn: 60 * 5 })
        res.send({ code: 200, msg: '登录成功', userinfo: arr[i], token })
    } else {
        res.send({ code: 400, mag: '登录失败' })

    }
})

//分页
userRouter.get('/list', (req, res) => {
    var { page, count } = req.query;
    var filepath = path.join(__dirname, '../json/users.json');
    var arr = fs.readFileSync(filepath);
    arr = arr.toString() ? JSON.parse(arr) : []

    //分页
    var newarr = arr.slice((page - 1) * count, page * count);
    //返回数据
    res.send({ code: 200, msg: '请求成功!', list: newarr });
})

//注册
userRouter.post('/register', (req, res) => {
    var { phone } = req.body
    var filepath = path.join(__dirname, '../json/users.json')
    var arr = fs.readFileSync(filepath)
    arr = arr.toString() ? JSON.parse(arr) : []

    var i = arr.findIndex((item) => {
        return item.phone == phone
    })
    if (i != -1) {
        res.send({ code: 400, msg: '该手机号已注册，注册失败' })
    } else {
        arr.push({ ...req.body })
        fs.writeFileSync(filepath, JSON.stringify(arr), 'utf8')
        res.send({ code: 200, msg: '注册成功' })
    }

})
//新增
// userRouter.post('/add', (req, res) => {
//     var { mane,code} = req.body
//     var filepath = path.join(__dirname, '../json/add.json')
//     var ary = fs.readFileSync(filepath)
//     ary = ary.toString() ? JSON.parse(ary) : []
//     if (ary.findIndex((item) => {
//         return item.code == code
//     })==200){
//         data.push({ ...req.body }) 
//         fs.writeFileSync(filepath, JSON.stringify(data), 'utf8')
//         res.send({status:200,msg:'添加成功'})
//     }
// })

//模糊搜索
userRouter.get('/search', (req, res) => {
    var { searchkey } = req.query
    var filepath = path.join(__dirname, '../json/users.json')
    var arr = fs.readFileSync(filepath)
    arr = arr.toString() ? JSON.parse(arr) : []
    var arn = ary.filter((item) => {
        return item.name.indexOf(searchkey) != -1
    })
    res.send({ code: 200, msg: '查询成功', list: arn })
})

userRouter.post('/delete', (req, res) => {

    var { phone } = req.body;


    var filepath = path.join(__dirname, '../json/users.json');
    var arr = fs.readFileSync(filepath);
    arr = arr.toString() ? JSON.parse(arr) : []


    var i = arr.findIndex((item) => {
        return item.phone == phone;
    })
    if (i != -1) {
        arr.splice(i, 1);
        fs.writeFileSync(filepath, JSON.stringify(arr), 'utf8');

        var newarr = arr.slice(0, 5);

        res.send({ code: 200, msg: '删除成功!', list: newarr })
    } else {
        res.send({ code: 400, msg: '删除失败!' })
    }
})










module.exports = userRouter
